<template>
  <div>

<el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>商品分类</el-breadcrumb-item>
</el-breadcrumb>

<el-card>
  <el-button type="primary" @click="add">添加分类</el-button>

<!-- 表格 -->
<el-table
    :data="categories"
    style="width: 100%;margin-bottom: 20px;"
    row-key="cat_id"
    border
    default-expand-all
    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
    <el-table-column
      type="index"
      label="#"
      width="180">
    </el-table-column>
    <el-table-column
      prop="cat_name"
      label="分类名称"
      width="180">
    </el-table-column>
    <el-table-column
      prop="cat_deleted"
      label="是否有效"
      width="180">
      <template>
        <el-button type="success" icon="el-icon-check" size="mini" circle></el-button>
      </template>
    </el-table-column>
    <el-table-column
      prop="cat_level"
      label="排序">
      <template slot-scope="scope">

<el-tag v-if="scope.row.cat_level==0">一级</el-tag>
<el-tag v-if="scope.row.cat_level==1" type="success">二级</el-tag>
<el-tag v-if="scope.row.cat_level==2" type="info">三级</el-tag>

      </template>
    </el-table-column>
    <el-table-column
      prop="address"
      label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>


 <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>



</el-card>



<!-- 添加分类 -->
<el-dialog title="添加分类" :visible.sync="dialogFormVisible">
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="分类名称" prop="cat_name">
    <el-input v-model="ruleForm.cat_name"></el-input>
  </el-form-item>
  <el-form-item label="父级分类" >
    <el-cascader
    v-model="value"
    :options="options"
    :props="{ expandTrigger: 'click',label:'cat_name' ,value:'cat_id',checkStrictly:true}"
    @change="handleChange"></el-cascader>

  </el-form-item>
</el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
  </div>
</el-dialog>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        value:[],
        options:[],
        categories:[],
        pagenum:1,
        pagesize:3,
        total:0,
        dialogFormVisible:false,
         ruleForm: {
          cat_name: '',
          cat_pid:0 ,   //父级id
          cat_level:0   //分类层级
        },
        rules: {
          cat_name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
        }
      }
    },
    created(){
      this.getdata()
    },
    methods: {
       submitForm(formName) {  //点击弹框的确定按钮
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
           console.log(this.ruleForm,9)

           this.$axios.post('categories',this.ruleForm)
           .then(res=>{
             console.log(res,"fj")
             if(res.meta.status==201){
               this.$message("成功")
               this.dialogFormVisible=false
               this.getdata()
             }
           })


          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      add(){
        this.dialogFormVisible=true
        // axios获取父级分类数据
        this.$axios.get('categories',{
          params:{
            type:2
          }
        })
        .then(res=>{
          console.log(res.data,0)
          this.options=res.data
        })
      },
      handleChange(value) {
        console.log(value,"change");

        this.ruleForm.cat_pid=value[value.length-1]
        this.ruleForm.cat_level=value.length
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pagesize=val
        this.getdata()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pagenum=val
        this.getdata()
      },
      getdata(){
          this.$axios.get('categories',{
            params:{
              pagenum:this.pagenum,
              pagesize:this.pagesize
            }
          })
          .then(res=>{
             console.log(res,'fl')
             this.categories=res.data.result
             this.total=res.data.total
          })
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      }
    }
  }
</script>

<style scoped>

</style>